package com.halflife.comeElectricity.basic.datadic;

import com.halflife.framework.converter.MapHandler;
import com.halflife.framework.domain.Page;
import com.halflife.framework.participant.Department;
import com.halflife.framework.participant.user.UserView;
import com.halflife.shiro.utils.ShiroUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class DataDicAdminController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private DataDicRepository dataDicRepository;
    @Resource
    private DataDicTypeRepository dataDicTypeRepository;

    @RequestMapping(value = {"/dataDic/list","/dataDic/list/{type}"})
    public String gotoList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model,@PathVariable(required = false) String type) {
        MapHandler.handleMap(queryParam);
        if (page == null) {
            page = new Page();
        }
        page.setQueryParam(queryParam);
        queryParam.put("valid", true);
        if (StringUtils.isNotBlank(type)) {
            Integer dataDicTypeId = Integer.valueOf(type);
            DataDicType dataDicType = dataDicTypeRepository.selectByPrimaryKey(dataDicTypeId);
            model.addAttribute("dataDicType", dataDicType);
            model.addAttribute("type", type);
            queryParam.put("dataDicTypeId", dataDicTypeId);
        }else{
            throw new RuntimeException("type is blank");
        }
        int totalCount = dataDicRepository.selectCount(queryParam);
        List<DataDic> list = dataDicRepository.selectListByPage(queryParam);
        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        model.addAttribute("catagory",queryParam.get("catagory"));
        model.addAttribute("departmentId",queryParam.get("departmentId"));
        return "dataDic/list";
    }

    @RequestMapping("/dataDic/operate")
    public String gotoOperate(Integer departmentId,String catagory,Integer id,Integer dataDicTypeId,HttpServletResponse response,
                               HttpServletRequest request, Model model) {
        DataDic dataDic = new DataDic();
        if(id!=null){
            dataDic = dataDicRepository.selectByPrimaryKey(id);
        }
        else{
            if (dataDicTypeId != null) {
                dataDic.setDataDicTypeId(dataDicTypeId);
            }
            dataDic.setCatagory(catagory);
            dataDic.setDepartmentId(departmentId);
            if (departmentId != null) {
                UserView userView = ShiroUtil.getUserView(request);
                if (userView.getDepartmentId() != departmentId) {
                    throw new RuntimeException("系统异常:登陆人的Department与业务Department不同");
                }
                Department department = userView.getDepartment();
                dataDic.setDepartment(department);
            }
        }
        model.addAttribute("dataDic",dataDic);
        if ("charge".equals(catagory)) {
            return "dataDic/charge";
        }
        if ("deposit".equals(catagory)) {
            return "dataDic/deposit";
        }
        return "dataDic/operate";
    }

    @PostMapping("/dataDic/save")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity save(DataDic dataDic, HttpServletRequest request,
                              HttpServletResponse response, Model model) {
        try {
            if(dataDic.getId()!=null){
                dataDicRepository.updateByPrimaryKey(dataDic);
            }else {
                dataDic.init();
                Integer a = dataDicRepository.insert(dataDic);
            }
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }

    @DeleteMapping("/dataDic/delete")
    public ResponseEntity setRiderState(Integer id, Integer state) {
        try {
            DataDic dataDic = new DataDic();
            dataDic.setId(id);
            dataDic.setValid(Boolean.FALSE);
            dataDicRepository.updateByPrimaryKeySelective(dataDic);
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
}
